package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConstant;
import com.itheima.common.exception.CheckItemException;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.QueryPageBean;
import com.itheima.pojo.Result;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @version V1.0
 * @author: yongjichu
 * @date: 2020/1/9 21:07
 * @description:
 */

/**
 * 体检检查项的数据的添加
 */
@RequestMapping("/checkitem")
@RestController
public class CheckItemController {
    @Reference(cluster = "failfast")
    private CheckItemService checkItemService;

    /**
     * 添加检查项
     * @param checkItem
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    /**
     * 根据条件进行查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/queryPage")
    public PageResult queryPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = checkItemService.queryPage(queryPageBean);
            return new PageResult(pageResult.getTotal(), pageResult.getRows(), MessageConstant.QUERY_CHECKITEM_SUCCESS, true);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(MessageConstant.QUERY_CHECKITEM_FAIL, false);
        }
    }

    /**
     * 根据id进行数据的删除
     *这个用户必须得有CHECKITEM_DELETE权限才能进行这个删除的逻辑
     * @param id
     * @return
     */

    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        try {
            checkItemService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (CheckItemException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    /**
     * 根据id查询数据，用于数据编辑时的数据回显
     *
     * @param id
     * @return
     */
    @RequestMapping("/queryById")
    public Result queryById(Integer id) {
        try {
            CheckItem checkItem = checkItemService.queryById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 获取表单的信息根据表单中的id进行数据的的更新
     *
     * @param checkItem
     * @return
     */
    @RequestMapping("/updataById")
    public Result updataById(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.updataById(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 查询所有的数据
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckItem> checkItemList=checkItemService.findAll();
            if(checkItemList!=null&&checkItemList.size()>0){
                Result result= new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS);
                result.setData(checkItemList);
                return result;
            }else {
                return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
